import 'package:f200/common/index.dart';
import 'package:flutter/material.dart';

import '../../domain/goods_info_model.dart';

class GoodsItemWidget extends StatelessWidget {
  final bool border;
  final String? activeDesc;
  final VoidCallback? onPress;
  final GoodsInfoModel goods;
  final VoidCallback? buyBtnClick;

  /// 创建商品
  ///
  /// ```
  /// @param {bool} border - 是否显示底部的border
  /// @param {String} activeDesc - 活动描述
  /// @param {Function} onPress - 点击转到产品详情页面
  /// @param {GoodsListDatum} data
  /// ```
  GoodsItemWidget({
    super.key,
    required this.goods,
    this.border = true,
    this.activeDesc,
    this.onPress,
    this.buyBtnClick,
  });

  @override
  Widget build(BuildContext context) {
    // var itemProvier = ref.read(CartItemProvider(goods.id.toString()));
    var w = Row(
      children: <Widget>[
        Expanded(
          child: Container(
            padding: const EdgeInsets.symmetric(vertical: 12),
            decoration: BoxDecoration(
              border: cuBorderBottom(show: border),
            ),
            child: Row(
              children: <Widget>[
                GestureDetector(
                  onTap: onPress,
                  child: SizedBox(
                    width: 80,
                    height: 80,
                    child: Stack(
                      children: [
                        Positioned(child: goodsImg(goods.pic)),
                        activeMsg(text: activeDesc),
                      ],
                    ),
                  ),
                ),
                Expanded(
                  child: Container(
                    margin: const EdgeInsets.only(left: 4),
                    child: Column(
                      mainAxisAlignment: MainAxisAlignment.start,
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        GestureDetector(
                          onTap: onPress,
                          child: goodsTitle(
                            goods.name,
                            color: const Color.fromRGBO(56, 56, 56, 1),
                          ),
                        ),
                        goodsTitle(
                          goods.characteristic.isEmpty
                              ? ''
                              : goods.characteristic,
                          fontSize: 12,
                        ),
                        goodsTitle(""),
                        Container(
                          margin: const EdgeInsets.only(bottom: 4),
                          child: Row(
                            mainAxisAlignment: MainAxisAlignment.spaceBetween,
                            crossAxisAlignment: CrossAxisAlignment.end,
                            children: [
                              Text(
                                '¥ ${goods.price}',
                                style: const TextStyle(
                                    color: Color.fromRGBO(244, 18, 18, 1),
                                    fontSize: 15,
                                    fontWeight: FontWeight.bold),
                              ),
                              SizedBox(width: 4),
                              Text(
                                '¥ ${goods.originalPrice}',
                                style: const TextStyle(
                                    color: Color.fromRGBO(130, 127, 127, 1),
                                    fontSize: 10,
                                    textBaseline: TextBaseline.ideographic,
                                    decoration: TextDecoration.lineThrough),
                              ),
                              Expanded(child: Text('')),
                              InkWell(
                                child: const SizedBox(
                                  width: 25,
                                  height: 25,
                                  child: Icon(
                                    Icons.shopping_cart_outlined,
                                    color: Color.fromRGBO(219, 140, 37, 1),
                                    size: 20,
                                  ),
                                ),
                                onTap: () {},
                              ),
                              SizedBox(width: 2),
                            ],
                          ),
                        )
                      ],
                    ),
                  ),
                )
              ],
            ),
          ),
        ),
      ],
    );
    return w;
  }

  /// 商品信息中的文本
  ///
  /// ```
  /// @param {String} title - 标题
  /// @param {double} fontSize
  /// @param {FontWeight} fontWeight
  /// @param {Color} color
  /// ```
  Widget goodsTitle(
    String title, {
    double fontSize = 16,
    FontWeight? fontWeight,
    Color? color,
  }) {
    return Row(
      mainAxisSize: MainAxisSize.min,
      children: <Widget>[
        Flexible(
          child: Text(
            title,
            overflow: TextOverflow.ellipsis,
            style: TextStyle(
              color: color ?? const Color.fromRGBO(166, 166, 166, 1),
              fontSize: fontSize,
              fontWeight: fontWeight ?? FontWeight.normal,
            ),
          ),
        )
      ],
    );
  }

  /// 商品图片
  Widget goodsImg(String imgSrc) {
    return ClipRRect(
      borderRadius: BorderRadius.circular(4.0),
      child: imgSrc.isNotEmpty
          ? Image.network(
              imgSrc,
              width: 100,
              height: 80,
              fit: BoxFit.cover,
            )
          : Container(
              color: const Color.fromARGB(255, 245, 245, 245),
              width: 100,
              height: 80,
              child: Image.asset(
                'assets/images/goods_detail1.jpg',
                fit: BoxFit.cover,
              ),
            ),
    );
  }

  ///活动信息
  Widget activeMsg({String? text}) {
    return text == null || text.isEmpty
        ? Container()
        : Positioned(
            right: 0,
            bottom: 5,
            child: Container(
              height: 16,
              padding: const EdgeInsets.symmetric(horizontal: 4),
              alignment: Alignment.center,
              decoration: BoxDecoration(
                borderRadius: BorderRadius.circular(2.0),
                color: const Color.fromRGBO(255, 129, 2, 1),
              ),
              child: Text(
                text,
                style: const TextStyle(
                  color: Colors.white,
                  fontSize: 8,
                ),
              ),
            ),
          );
  }
}
